<?php
/**
* Class Project into Home Controller
* @author SillySmart
* @copyright SillySmart
* @package Mvc.Controllers.Home
* @see Mvc.Controllers.Home.ControllerProtected
* @see Mvc.Controllers.SiteProtected
* @see Sls.Controllers.Core.SLS_GenericController
* @since 1.0
*
*/
class HomeProject extends HomeControllerProtected
{
	public function init()
	{
		parent::init();
	}

	public function action()
	{
		$qbd = new SLS_QueryBuilder();
		$xml = $this->getXml();
		$this->useModel('project');
		$project = new Main_Project();
		$lang = $this->_lang->getLang();

		$projectId = $this->_http->getParam('Id');
		if($project->getModel($projectId) === false)
			$this->forward('Default', 'UrlError');

		$this->setMetaTitle($project->project_name);

		# Projects
		$xml->startTag('projects');
		$projects = $qbd->select('*')
			->from('project')
			->where($qbd->expr()->eq('pk_lang', $lang))
			->order('project_id', 'desc')
			->execute();
		$nbProjects = count($projects);

		$i = 0;
		foreach($projects as $p)
		{
			$xml->startTag('project', array('current' => $p->project_id == $project->project_id ? 'true' : 'false'));
				$xml->addFullTag('project_id', $p->project_id, true);

				if($p->project_id == $project->project_id)
				{
					foreach($p as $key => $value)
					{
						if($key == 'project_img')
							$value = SLS_String::getUrlFile($value);
						$xml->addFullTag($key, $value, true);
					}

					# Controls
					if($i == 0) $iPrev = $nbProjects-1;
					else $iPrev = $i-1;

					if($i == $nbProjects-1) $iNext = 0;
					else $iNext = $i+1;

					$xml->startTag('project_previous');
						$xml->addFullTag('project_id', $projects[$iPrev]->project_id, true);
						$xml->addFullTag('project_url', $this->_generic->getFullPath('Home', 'Project', array(array('key' => 'Id', 'value' => $projects[$iPrev]->project_id))), true);
						$xml->addFullTag('project_name', $projects[$iPrev]->project_name, true);
					$xml->endTag('project_previous');

					$xml->startTag('project_next');
						$xml->addFullTag('project_id', $projects[$iNext]->project_id, true);
						$xml->addFullTag('project_url', $this->_generic->getFullPath('Home', 'Project', array(array('key' => 'Id', 'value' => $projects[$iNext]->project_id))), true);
						$xml->addFullTag('project_name', $projects[$iNext]->project_name, true);
					$xml->endTag('project_next');
					# /Controls

					# Devices Slider
					$xml->startTag('project_device_slider');
						$devicesItems = $qbd->select('*')
							->from('project_device_slider')
							->where($qbd->expr()->eq('project_id',$project->project_id))
							->order('project_device_slider_id', 'asc')
							->execute();

						$desktopItems = array_filter($devicesItems, function($i){
							if($i->project_device_slider_type == 'desktop')
								return true;
							return false;
						});
						$nbDesktopItems = count($desktopItems);

						$tabletItems = array_filter($devicesItems, function($i){
							if($i->project_device_slider_type == 'tablet')
								return true;
							return false;
						});
						$nbTabletItems = count($tabletItems);

						$mobileItems = array_filter($devicesItems, function($i){
							if($i->project_device_slider_type == 'mobile')
								return true;
							return false;
						});
						$nbMobileItems = count($mobileItems);

						if($nbDesktopItems && $nbTabletItems && $nbMobileItems)
						{
							$format = 'dtm';
							$formatLabel = $GLOBALS[$GLOBALS['PROJECT_NAME']]['XSL']['PROJECT_DEVICE_DESKTOP'].', '.$GLOBALS[$GLOBALS['PROJECT_NAME']]['XSL']['PROJECT_DEVICE_TABLET'].' & '.$GLOBALS[$GLOBALS['PROJECT_NAME']]['XSL']['PROJECT_DEVICE_MOBILE'];
						}
						else if($nbDesktopItems && $nbTabletItems)
						{
							$format = 'dt';
							$formatLabel = $GLOBALS[$GLOBALS['PROJECT_NAME']]['XSL']['PROJECT_DEVICE_DESKTOP'].' & '.$GLOBALS[$GLOBALS['PROJECT_NAME']]['XSL']['PROJECT_DEVICE_TABLET'];
						}

						else if($nbDesktopItems && $nbMobileItems)
						{
							$format = 'dm';
							$formatLabel = $GLOBALS[$GLOBALS['PROJECT_NAME']]['XSL']['PROJECT_DEVICE_DESKTOP'].' & '.$GLOBALS[$GLOBALS['PROJECT_NAME']]['XSL']['PROJECT_DEVICE_MOBILE'];
						}

						else if($nbTabletItems && $nbMobileItems)
						{
							$format = 'tm';
							$formatLabel = $GLOBALS[$GLOBALS['PROJECT_NAME']]['XSL']['PROJECT_DEVICE_TABLET'].' & '.$GLOBALS[$GLOBALS['PROJECT_NAME']]['XSL']['PROJECT_DEVICE_MOBILE'];
						}

						else if($nbDesktopItems)
						{
							$format = 'd';
							$formatLabel = $GLOBALS[$GLOBALS['PROJECT_NAME']]['XSL']['PROJECT_DEVICE_DESKTOP'];
						}

						else if($nbTabletItems)
						{
							$format = 't';
							$formatLabel = $GLOBALS[$GLOBALS['PROJECT_NAME']]['XSL']['PROJECT_DEVICE_TABLET'];
						}
						else if($nbMobileItems)
						{
							$format = 'm';
							$formatLabel = $GLOBALS[$GLOBALS['PROJECT_NAME']]['XSL']['PROJECT_DEVICE_MOBILE'];
						}

						$xml->addFullTag('project_device_slider_format', $format, true);
						$xml->addFullTag('project_device_slider_format_label', ucfirst(strtolower($formatLabel)), true);

						$xml->startTag('project_device_slider_items');
							foreach($devicesItems as $devicesItem)
							{
								$xml->startTag('project_device_slider_item');
									foreach($devicesItem as $key => $value)
									{
										if($key == 'project_device_slider_img')
											$value = SLS_String::getUrlFile($value);
										$xml->addFullTag($key, $value, true);
									}
								$xml->endTag('project_device_slider_item');
							}
						$xml->endTag('project_device_slider_items');

					$xml->endTag('project_device_slider');
					# /Devices Slider

					# Project Slider
					$xml->startTag('project_slider');
						$projectItems = $qbd->select('*')
							->from('project_slider')
							->where($qbd->expr()->eq('project_id',$project->project_id))
							->order('project_slider_id', 'asc')
							->execute();

						foreach($projectItems as $projectItem)
						{
							$xml->startTag('project_slider_item');
								foreach($projectItem as $key => $value)
								{
									if($key == 'project_slider_img')
										$value = SLS_String::getUrlFile($value);
									$xml->addFullTag($key, $value, true);
								}
							$xml->endTag('project_slider_item');
						}
					$xml->endTag('project_slider');
					# /Project Slider

					# Awards
					$xml->startTag('project_awards');
					$projectAwards = $qbd->select('*')
						->from('project_award')
						->where($qbd->expr()->eq('project_id',$project->project_id))
						->whereAnd($qbd->expr()->eq('pk_lang', $lang))
						->order('project_award_id', 'asc')
						->execute();

					foreach($projectAwards as $projectAward)
					{
						$xml->startTag('project_award');
						foreach($projectAward as $key => $value)
							$xml->addFullTag($key, $value, true);
						$xml->endTag('project_award');
					}
					$xml->endTag('project_awards');
					# /Awards
				}
			$xml->endTag('project');
			$i++;
		}
		$xml->endTag('projects');
		# /Projects

		$this->saveXml($xml);
	}
}
?>